"use strict";

GameUI.SetCameraTerrainAdjustmentEnabled( false );
function formatString(str,args){
	for(var i in args){
		var arg = args[i];
		str = str.replace("s"+i,arg);
	}
	return str;
} 

function localHiddenMsg(){
	$('#errorMsgBG').SetHasClass("AniScaleOut",false);
	$('#errorMsgBG').SetHasClass("Hidden",true);
}
function localShowMsg(){
	$('#errorMsgBG').SetHasClass("AniScaleOut",true);
	$('#errorMsgBG').SetHasClass("Hidden",false);
}
var g_func_hiddenClass = null;
function onErrorMsg(data){
	var text = data.text
	var errorMsgBG = $('#errorMsgBG');
	Game.EmitSound( "General.CastFail_AbilityNotLearned" );
	if (errorMsgBG.BHasClass("AniScaleOut")){
		localHiddenMsg();
		$.CancelScheduled(g_func_hiddenClass);
	}
	localShowMsg();
	$('#errorMsgTxt').text = $.Localize(text);
	g_func_hiddenClass = $.Schedule(1.2,localHiddenMsg);
}
GameUI.onErrorMsg = onErrorMsg;
GameEvents.Subscribe( "S2C_ERROR_MSG", onErrorMsg );
localHiddenMsg();

//下面这句只能放在这里，不然自定义的DOTAMinimap没法生效
//GameUI.SetDefaultUIEnabled( DotaDefaultUIElement_t.DOTA_DEFAULT_UI_ACTION_MINIMAP, false );



// function onGotoEnhance(data){
	

// 	$("#hammer").visible = true;
// 	$.Schedule(0.1,updateHammer);
// }

// function updateHammer(){
// 	var mPos = GameUI.GetCursorPosition();
//     var GamePos = Game.ScreenXYToWorld(mPos[0], mPos[1]);
    
//     var screenPosX = Game.WorldToScreenX( GamePos[0],GamePos[1],GamePos[2] );
// 	var screenPosY = Game.WorldToScreenY( GamePos[0],GamePos[1],GamePos[2] );
// 	var width = $('#hammer').actuallayoutwidth;
// 	var height = $('#hammer').actuallayoutheight;
// 	$("#hammer").style.x = (screenPosX-width/2)+"px";
// 	$("#hammer").style.y = (screenPosY-height/2)+"px";
// 	$.Msg(screenPosX+"  "+screenPosY);
// 	$.Schedule(0.1,updateHammer);
// }

// function leaveEnhance(data){
// 	$("#hammer").visible = false
// 	$.CancelScheduled(updateHammer);
// }
// GameEvents.Subscribe( "S2C_GOTO_ENHANCE", onGotoEnhance );

function S2C_REMOVE_TIME(data){
	var panel = $('#'+data.index);
	if(panel!=null){
		panel.RemoveAndDeleteChildren();
		panel.DeleteAsync(0);
		Game.EmitSound( "General.CompendiumLevelUpMinor" );
	}
}
function doChangeTimer(panel,data){
	var TimerTick = panel.FindChildTraverse("TimerTick");
	var ProgressBarLeft = panel.FindChildTraverse("ProgressBarLeft");
	var left = data.total - data.cur;
	TimerTick.text = left;
	ProgressBarLeft.style.width = left / data.total * 100 + "%";
}
function S2C_CHANGE_TIME(data){
	var parent = $('#'+data.index)
	if(parent!=null){
		doChangeTimer(parent,data);
	}else{
		var panel = $.CreatePanel("Panel", $('#TimerPanel'), data.index);
		panel.BLoadLayoutSnippet("timerbar");
		var str = formatString($.Localize(data.str),data.args);
		var TimerLabel = panel.FindChildTraverse("TimerLabel");
		TimerLabel.text = str;
		doChangeTimer(panel,data);
		Game.EmitSound( "compendium_points" );
	}
	
}
GameEvents.Subscribe( "S2C_REMOVE_TIME", S2C_REMOVE_TIME );
GameEvents.Subscribe( "S2C_CHANGE_TIME", S2C_CHANGE_TIME );
function BTN_SHOUQI(){
	if($('#InfoList').BHasClass("ZeroHeight")){
		$('#InfoList').style.height = "0px";
	}else{
		$('#InfoList').style.height = "250px";
	}
	$('#InfoList').SetHasClass("ZeroHeight",!$('#InfoList').BHasClass("ZeroHeight"));
	
}


var txtList = [
	"none",
	"mwb",
	"jsqd",
	"wlqd",
	"fsqd",
	"dh",
	"xf",
	"xyz",
	"jj",
	"wx",
	"vip",
	"gold",
	"diamond",
	//"fasqd"
	"htzy",
];

function ShowIconName(idx){
	$.DispatchEvent( "DOTAShowTextTooltip",$('#info'+idx), $.Localize("#"+txtList[idx]));
}

function HideIconName(){
	$.DispatchEvent( "DOTAHideTextTooltip" );
}

function C2S_GET_ROLE_INFO(data){
	for(var i in data){
		var val = data[i];
		if ($('#lb_'+i)!=null){
			if(i=="dh"){
				val = Math.floor(val / 365)+$.Localize("#TIP_YEAR")+val%365+$.Localize("#TIP_DAY");
			}
			$('#lb_'+i).text = val;
		}
	}
}

function UpdateProperty(){
	var unit = Players.GetLocalPlayerPortraitUnit();
	if(!Entities.IsRealHero( unit)){
		return;
	}
	$('#InfoRoot').visible = true;
	var roleData = CustomNetTables.GetTableValue( "roleData", unit.toString() );
	var custom_property = CustomNetTables.GetTableValue( "custom_property", unit.toString() );
	for(var i in roleData){
		var val = roleData[i]+(custom_property["attr_"+i]||0);
		if ($('#lb_'+i)!=null){
			if(i=="dh"){
				val = Math.floor(val / 365)+$.Localize("#TIP_YEAR")+val%365+$.Localize("#TIP_DAY");
			}
			$('#lb_'+i).text = val;
		}
	}
}
CustomNetTables.SubscribeNetTableListener( "custom_property", UpdateProperty );
CustomNetTables.SubscribeNetTableListener( "roleData", UpdateProperty );

GameEvents.Subscribe( "C2S_GET_ROLE_INFO", C2S_GET_ROLE_INFO ); 
//这里很坑爹！！！必须延迟一点时间才能起作用
function S2C_SELECT_UNIT(data){
	$.Schedule(0.2,function(){
		GameUI.SelectUnit( data.unit, false );
	})
}
GameEvents.Subscribe( "S2C_SELECT_UNIT", S2C_SELECT_UNIT );

function addButton(){
	var cur = $.GetContextPanel().GetParent().GetParent().GetParent();
	var d = cur.FindChildTraverse('GlyphScanContainer');
	// d.style["flow-children"] = "down";
	d.RemoveAndDeleteChildren();
	var zuoji = $.CreatePanel("Panel",d,"btn_btns.xml"); 
	    zuoji.BLoadLayout("file://{resources}/layout/custom_game/btn_btns.xml",false,false);
}
addButton();
 
function S2C_UPDATE_LQEXP(data){
	$('#lb_lqexp').text = data.exp;
}
GameEvents.Subscribe( "S2C_UPDATE_LQEXP", S2C_UPDATE_LQEXP );

function MouseOver(str,panelname){
	$.DispatchEvent( "DOTAShowTextTooltip",$('#btn_'+str), $.Localize(str));
}

function MouseOut(){
	$.DispatchEvent( "DOTAHideTextTooltip" );
}

function ShowResPanel(){
	$('#kill_num').visible = !$('#kill_num').visible;
}

function refresh_diff(){
	var config = CustomNetTables.GetTableValue( "config","config" );
	var diff = config.diff;
	var mode = config.mode;
	$('#diff_label').text = $.Localize("#diff"+diff);
	var q = ["","GAMEMODE_NORMAL","GAMEMODE_FAST"];
	$('#mode_label').text = $.Localize(q[mode]);
}
refresh_diff();
//GameEvents.Subscribe( "refresh_diff", refresh_diff );
GameEvents.SendCustomGameEventToServer( "refresh_diff", {} );

function ShowBuildPanel(){
	$('#setRoot2').visible = !$('#setRoot2').visible;
}



(function()
{
	//GameEvents.SendCustomGameEventToServer( "C2S_REMOVEPAR", {} );
	GameEvents.SendCustomGameEventToServer( "C2S_RECONNECT", {} );
})();